﻿using Pathfinding;
using System;
using UnityEngine;
using UnityEngine.Networking;

[RequireComponent(typeof(Move))]
public class AIMove : AIBehaviour
{
    private Vector3 destPos;
    private bool isResting;
    private float lastEscapeTime = -1f;
    private SearchNode path;
    private Vector3 vaildDestPos;
    private Vector3 vaildSelfPos;

    private bool HasObstableBetweenTarget()
    {
        return ((this.target != null) && Physics.Linecast(base.position.SetY(0.5f), this.target.transform.position.SetY(0.5f), (int) (GameLayers.Obstacle | GameLayers.Wall)));
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        base.OnDeserialize(reader, initialState);
    }

    private void OnDrawGizmosSelected()
    {
        if (this.pathfinder != null)
        {
            foreach (Point3D pointd in this.pathfinder.GetAllWalkable())
            {
                Gizmos.color = Color.yellow;
                Gizmos.DrawWireCube((Vector3) pointd, (Vector3) (Vector3.one * 0.2f));
            }
        }
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        bool flag;
        bool flag2 = base.OnSerialize(writer, forceAll);
        return (flag | flag2);
    }

    protected override void Start()
    {
        base.Start();
        this.destPos = base.position;
        this.StartRandomRest();
    }

    private void StartRandomRest()
    {
        Action onFinished = delegate {
            this.isResting = false;
            this.StartWaitForSeconds((float) UnityEngine.Random.Range(1, 5), new Action(this.StartRandomRest));
        };
        this.isResting = true;
        this.StartWaitForSeconds(UnityEngine.Random.Range((float) 0.1f, (float) 0.3f), onFinished);
    }

    private void UNetVersion()
    {
    }

    private void Update()
    {
        if ((this.pathfinder != null) && (this.level != null))
        {
            if (base.isMonster && base.isWeaponCoolingDown)
            {
                this.direction = Vector3.zero;
            }
            else if (this.target == null)
            {
                this.direction = Vector3.zero;
            }
            else
            {
                if (this.target.gameObject.layer == GameLayers.Item.index)
                {
                    this.destPos = this.target.transform.position;
                }
                else if ((base.aiBase.weapon != null) && (!base.aiBase.canWeaponHitTarget || this.HasObstableBetweenTarget()))
                {
                    this.destPos = this.target.transform.position;
                }
                else if ((Time.time - this.lastEscapeTime) > 1f)
                {
                    this.destPos = this.level.GetRandomWalkable();
                    this.lastEscapeTime = Time.time;
                }
                this.destPos.y = 0f;
                if (this.pathfinder.IsPositionFree(base.position))
                {
                    this.vaildSelfPos = base.position;
                }
                if (this.pathfinder.IsPositionFree(this.destPos))
                {
                    this.vaildDestPos = this.destPos;
                }
                if ((Time.frameCount % UnityEngine.Random.Range(60, 240)) == 0)
                {
                    this.path = this.pathfinder.FindPath(this.vaildSelfPos, this.vaildDestPos);
                }
                if ((this.path != null) && (this.path.next != null))
                {
                    this.destPos = (Vector3) this.path.next.position;
                    if (this.arrived)
                    {
                        this.path = this.path.next;
                    }
                }
                Vector3 zero = this.destPos - base.position;
                if (this.arrived)
                {
                    zero = Vector3.zero;
                }
                if (this.target == null)
                {
                    zero = Vector3.zero;
                }
                if (this.isResting)
                {
                    zero = Vector3.zero;
                }
                this.direction = zero;
            }
        }
    }

    private bool arrived
    {
        get
        {
            Vector3 vector = this.destPos - base.position;
            return (vector.magnitude < 0.1f);
        }
    }

    public Vector3 direction
    {
        get
        {
            return base.move.direction;
        }
        set
        {
            base.move.direction = value;
        }
    }

    public bool hasTarget
    {
        get
        {
            return (this.target != null);
        }
    }

    private Level level
    {
        get
        {
            return Level.current;
        }
    }

    private Pathfinder pathfinder
    {
        get
        {
            return ((Level.current == null) ? null : Level.current.pathfinder);
        }
    }

    private GameObject target
    {
        get
        {
            return ((base.targetDetector == null) ? null : base.targetDetector.target);
        }
    }
}

